package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/gofrs/uuid"
	"github.com/gorilla/sessions"
	"project/study-go/ecode"
	"project/study-go/http/middleware"
	"project/study-go/logic"
	"project/study-go/model"
	"project/study-go/validate"
)

type TopicController struct{}

func (t *TopicController) RegisterRoute(g *gin.RouterGroup) {
	topic_node := g.Group("/topic_node")
	{
		topic_node.GET("", middleware.SessionAuth(), ListTopicNode)
		topic_node.GET("/:id", middleware.SessionAuth(), ListTopic)
		topic_node.POST("", middleware.SessionAuth(), CreateTopicNode)
	}

	topic := g.Group("/topic")
	{
		topic.GET("/:id", middleware.SessionAuth(), GetTopic)
		topic.POST("", middleware.SessionAuth(), CreateTopic)
	}
}

//ListTopicNode 列出帖子节点（板块）
func ListTopicNode(c *gin.Context) {
	topicNodes, errCode := logic.DefaultTopic.ListTopicNode()
	if errCode.Err != nil {
		c.JSON(errCode.Code, gin.H{
			"code": errCode.Code,
			"msg":  errCode.Err.Error(),
		})
		return
	}

	c.JSON(errCode.Code, gin.H{
		"code": errCode.Code,
		"data": topicNodes,
	})
}

//ListTopic 列出节点下的所有帖子
func ListTopic(c *gin.Context) {
	params := make(map[string]interface{})
	nodeId := c.Param("id")
	if nodeId == "" {
		c.JSON(400, ecode.Response{
			Code: 400,
			Msg:  ecode.ErrBadParam,
			Data: nil,
		})
		return
	}
	params["nid"] = nodeId

	//校验分页信息
	page := &model.Pageing{}
	err := c.ShouldBindQuery(page)
	if err != nil {
		c.JSON(400, ecode.Response{
			Code: 400,
			Msg:  validate.ValidateTranslation(err),
			Data: nil,
		})
		return
	}
	params["page"] = page.Page
	params["page_size"] = page.PageSize
	params["order_by"] = c.DefaultQuery("order_by", "create_at")

	count, topics, errCode := logic.DefaultTopic.ListTopics(params)
	if errCode.Err != nil {
		c.JSON(errCode.Code, ecode.Response{
			Code: errCode.Code,
			Msg:  errCode.Err,
			Data: nil,
		})
		return
	}

	data := make(map[string]interface{})
	data["list"] = topics
	data["total"] = count
	c.JSON(200, ecode.Response{
		Code: 200,
		Msg:  "success",
		Data: data,
	})
}

//NewTopic_Node 创建topic_node
func CreateTopicNode(c *gin.Context) {
	//表单参数校验
	node := &model.TopicNode{}
	err := c.ShouldBind(node)
	if err != nil {
		c.JSON(400, ecode.Response{
			Code: 400,
			Msg:  validate.ValidateTranslation(err),
			Data: nil,
		})
		return
	}

	//查询主题节点名是否已存在
	errCode := logic.DefaultTopic.TopicNodeNameExist(node.Name)
	if errCode.Err != nil {
		c.JSON(errCode.Code, gin.H{
			"code": errCode.Code,
			"msg":  errCode.Err.Error(),
		})
		return
	}

	//入库创建帖子节点
	node.Nid = uuid.Must(uuid.NewV4()).String()
	errCode = logic.DefaultTopic.CreateTopicNode(node)
	if errCode.Err != nil {
		c.JSON(errCode.Code, gin.H{
			"code": errCode.Code,
			"msg":  errCode.Err.Error(),
		})
		return
	}

	c.JSON(errCode.Code, gin.H{
		"code": errCode.Code,
		"msg":  "创建主题节点成功",
	})
}

//GetTopic 获取帖子
func GetTopic(c *gin.Context) {
	topicId := c.Param("id")
	if topicId == "" {
		c.JSON(400, ecode.Response{
			Code: 400,
			Msg:  ecode.ErrBadParam,
			Data: nil,
		})
		return
	}

	//入库，查询帖子
	topic, errCode := logic.DefaultTopic.GetTopic(topicId)
	if errCode.Err != nil {
		c.JSON(errCode.Code, ecode.Response{
			Code: errCode.Code,
			Msg:  errCode.Err.Error(),
			Data: nil,
		})
		return
	}

	c.JSON(errCode.Code, ecode.Response{
		Code: errCode.Code,
		Msg:  "成功获取帖子",
		Data: topic,
	})
}

//CreateTopic 创建帖子
func CreateTopic(c *gin.Context) {
	topic := &model.Topic{}
	err := c.ShouldBind(topic)
	if err != nil {
		c.JSON(400, ecode.Response{
			Code: 400,
			Msg:  validate.ValidateTranslation(err),
			Data: nil,
		})
		return
	}

	//创建入库
	topic.Tid = uuid.Must(uuid.NewV4()).String()
	session, _ := c.Get("SESSIONID")
	topic.Uid = session.(*sessions.Session).Values["uid"].(string)

	errCode := logic.DefaultTopic.CreateTopic(topic)
	if errCode.Err != nil {
		c.JSON(errCode.Code, ecode.Response{
			Code: errCode.Code,
			Msg:  errCode.Err.Error(),
			Data: nil,
		})
		return
	}

	c.JSON(errCode.Code, ecode.Response{
		Code: errCode.Code,
		Msg:  "创建帖子成功",
		Data: nil,
	})
}
